/**
 * Created by Admin on 2018/1/4.
 */
define(function (require, exports, module) {
    var template = require("template");
    var loadingTpl = require("../common/tpl/loading.html");
    var tpl = require("./tpl/areaStaticTpl.html");
    require("../common/highchart/highcharts");
    require("../common/highchart/columnChart");
    require("../common/highchart/piechart");
    require("../common/qy-template");
    var areaIpc = {
        initTable: function (apiUrl,exportUrl,keyName,selfTitle,quotatitleD) {
            $("#ptamountchart,#areaChart").html(template.compile(loadingTpl));
            $.ajax({
                url: window.jkconfig.webapiurlv + apiUrl+"?"+$("#searchForm").serialize(),
                success: function (data) {
                    $(".la-square-jelly-box.la-2x").hide();
                    if (data.code == 0 && data.infos != null  && data.infos.length > 1) {
                        $("#Js_statisticalBox").show();
                        $(".nodata-title").hide();
                        var html = template.compile(tpl)({data: data,keyName:keyName})
                        $("#validptchart-tab").html(html);
                        for (var i = 1, Name = [], fmcount = [], syxxcount = [], wgsjcount = []; i < data.infos.length; i++) {
                            Name.push(data.infos[i][keyName]);
                            fmcount.push(data.infos[i].fmcount);
                            syxxcount.push(data.infos[i].syxxcount);
                            wgsjcount.push(data.infos[i].wgsjcount);
                        }
                        var sourceData = [ {name: '发明专利', data: fmcount},
                                           {name: '实用新型', data: syxxcount},
                                           {name: '外观专利', data: wgsjcount}
                                         ],
                        //线形图初始化
                        linechart = new ColumnChart({
                            dom: "ptamountchart",
                            xData: Name,
                            yData: sourceData,
                            title:selfTitle,
                            quotatitleD: quotatitleD,
                            quotatitleDClass: "p-t-question-chart",
                            legend:{
                                layout: 'top',
                                align: 'right',
                                floating:false
                            }
                        });
                        /*subtitle数组*/
                        /*用于记录合计*/
                        var totalA = [];
                        $.each($(".totaleC"), function () { totalA.push($(this).text()); });
                        if (typeof(totalA[0]) == "undefined") { totalA[0] = 0; }
                        piedata = [];
                        for (var i = 0; i < data.infos.length; i++) {
                            var middleData = [];
                            middleData[0] = { name: '发明专利', y: data.infos[i].fmcount };
                            middleData[1] = { name: '实用新型', y: data.infos[i].syxxcount };
                            middleData[2] = { name: '外观专利', y: data.infos[i].wgsjcount};
                            piedata.push(middleData);
                        }
                        var piedtitle = "当前有效专利",
                            pietitle = piedtitle,
                            subtitle = "共" + totalA[0] + "件",
                            newPieChart = new PieChart({//饼图初始化
                                dom: "areaChart",
                                piedata: piedata,
                                pietitle: pietitle,//饼图一级标题
                                subtitle: subtitle,//饼图二级标题，
                                titleY:40,
                                subtitleY:60

                            }),
                            valChartTab = $("#validptchart-tab tr");//饼状图改变容器
                        var pieChange = new PieChange(valChartTab, false, true, newPieChart, totalA, pietitle, "所有区域");
                    }else{
                        $("#ptamountchart").html('<div class="nodata-title" style="height:270px;line-height:270px;">暂无数据</div>');
                        $("#Js_statisticalBox").hide();
                        $(".nodata-title").show();
                    }
                }
            });
            $("body").on("click",".btnExport",function(){
                window.location.href = window.jkconfig.webapiurlv + exportUrl+"?"+api_key+"="+token;
            })
        }
    }
    module.exports = areaIpc;
})